home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / searchbm.zip / BUFSEARC.PAS < prev    next >
Pascal/Delphi Source File  |  1993-06-12  |  3KB  |  131 lines

  1. { The originial benchmark program was to demonstrate the speed difference
  2.   between the POS() in Turbo Pascal 4 or 5 brute-force
  3.   and the Boyer-Moore method function POSBM()
  4.   Program author: Costas Menico }
  5.  
  6. (* Modifications for the use of an ARRAY of CHAR instead STRING buffer
  7.    made by Jochen Magnus 5/93
  8.  
  9.    REMARK: On 386/486 computers the advantage of the Boyer-Moore-method
  10.    is only considerable if using buffers > 1 KB. So the following
  11.    programm uses a 4K-Buffer which is initialized with random chars (JO).
  12.    Instead of comparing with the original Borland-POS, it uses a similar
  13.    brute search method for arrays of chars.
  14.  
  15.    Call: posbm(pat,buf,buflen);         
  16.    or if you are using a string buffer:
  17.          posbm(pat,s[1],length(s));
  18. *)
  19.  
  20. program bufSearch;
  21.  
  22. uses
  23.   dos, crt;
  24.  
  25.  
  26. function posbm(pat:string; var buf; buflen:word):word; EXTERNAL;
  27.  
  28. {$F+}
  29. {$L BM.OBJ}
  30. {$F-}
  31.  
  32.  
  33. function bruteForce(var such:string; var buf; buflen:word):word; ASSEMBLER;
  34. ASM
  35.     cld
  36.     push ds
  37.     les    di,buf
  38.     mov    cx,buflen
  39.     jcxz @@30
  40.     lds    si,such
  41.     mov  al,[si]
  42.     or   al,al
  43.     je   @@30
  44.     xor  ah,ah
  45.     cmp  ax,cx
  46.     ja   @@30
  47.     mov  bx,si
  48.     dec  cx
  49.   @@10:
  50.     mov  si,bx
  51.     lodsw
  52.     xchg al,ah          { AH=Stringlänge, AL=Suchchar }
  53.     repne scasb
  54.     jne  @@30
  55.     dec  ah
  56.     or   ah,ah
  57.     je   @@20
  58.  
  59.     inc  cx             { CX++ nach rep... }
  60.     xchg cx,ax
  61.     mov  cl,ch
  62.     xor  ch,ch
  63.     mov  dx,di
  64.     repe    cmpsb
  65.     mov  di,dx
  66.     mov  cx,ax
  67.     loopne @@10
  68.   @@20:
  69.     mov  ax,buflen
  70.     sub  ax,cx
  71.     dec  ax
  72.     jmp  @@40
  73.   @@30:
  74.     xor  ax,ax
  75.   @@40:
  76.     pop  ds
  77. end;
  78.  
  79.  
  80.  
  81. procedure showtime(s : string; t : registers);
  82.  
  83. begin
  84.   writeln(s, ' Hrs:', t.ch, ' Min:', t.cl, ' Sec:', t.dh, ' Milsec:', t.dl);
  85. end;
  86.  
  87. var
  88.   pat    : string;
  89.   i,
  90.   j      : integer;
  91.   start,
  92.   finish : registers;
  93.   arr    : array[1..4096] of char;
  94.  
  95. const
  96.   longloop = 5000;
  97.  
  98. begin
  99.   clrscr;
  100.   randomize;
  101.   for i := 1 to 4096 do
  102.     arr[i] := chr(random(255)+1);
  103.  
  104.   move(arr[4090],pat[1],5); pat[0]:=#5;
  105.  
  106.   writeln('Search using Brute-Force Method <please wait>');
  107.   start.ah := $2C;
  108.   msdos(start);
  109.   for j := 1 to longloop do
  110.     i := bruteForce(pat,arr,4096);
  111.   finish.ah := $2C;
  112.   msdos(finish);
  113.   showtime('Start  ', start);
  114.   showtime('Finish ', finish);
  115.   writeln('Pattern found at position ', i);
  116.   writeln;
  117.   writeln('Search using Boyer-Moore Method <please wait>');
  118.   start.ah := $2C;
  119.   msdos(start);
  120.   for j := 1 to longloop do
  121.     i := posbm(pat, arr,4096);
  122.   finish.ah := $2C;
  123.   msdos(finish);
  124.   showtime('Start  ', start);
  125.   showtime('Finish ', finish);
  126.   writeln('Pattern found at position ', i);
  127.   writeln;
  128.   writeln('Done ... Press Enter');
  129.   readln;
  130. end.
  131.